package com.hmc.modules.weka.controller;

import com.hmc.modules.sjfx.dto.glsjfx.GgHfDTO;
import com.hmc.modules.sjfx.service.lashent.LashentService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import weka.associations.Apriori;
import weka.associations.FPGrowth;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;

import java.text.ParseException;
import java.util.ArrayList;

/**
 * weka 关联关系挖掘：钢管焊缝抗拉强度
 */
@AllArgsConstructor
@RestController
@RequestMapping("weka/klqd")
@Tag(name = "关联关系挖掘：钢管焊缝抗拉强度")
public class WekaConteoller {
    @Autowired
    private LashentService lashentService;

    /**
     * 获取关联关系挖掘：钢管焊缝抗拉强度
     * @return
     */
    @RequestMapping("/getLashent")
    public String getLashent() throws Exception {
        ArrayList<GgHfDTO> data = lashentService.getGlsjfxKlqd();

        Instances instances = relationInstances(data);
        Apriori apriori = new Apriori();
        apriori.buildAssociations(instances);


        FPGrowth fpGrowth = new FPGrowth();
        fpGrowth.buildAssociations(instances);

        return null;
    }

    /**
     * 数据转换
     */
    private Instances relationInstances(ArrayList<GgHfDTO> list) {
        // 属性定义
        ArrayList<Attribute> attrRelation = new ArrayList<>();

        // -- 关系属性 1：数值型
        attrRelation.add(new Attribute("ys"));
        attrRelation.add(new Attribute("ts"));
        attrRelation.add(new Attribute("yieldRate"));
        attrRelation.add(new Attribute("el"));

        Instances data = new Instances("MyRelation", attrRelation, 0);

        list.forEach(item -> {
            data.add(new DenseInstance(1.0, new double[]{
                    Double.parseDouble(item.getYs()),
                    Double.parseDouble(item.getTs()),
                    Double.parseDouble(item.getYieldRate()),
                    Double.parseDouble(item.getEl())}));
        });

//        data.add(new DenseInstance(1.0, new double[]{1.0, 0.0, 0.0, 0.0}));

        // 4. 输出数据
//        System.out.println(data);

        return data;
    }
}
